<template>
	<view class="qidoongye">
		
	</view>
</template>

<script>
import {unreadMessageCount,InSignUnitList,signDetails} from "@/api";
import { getToken } from "@/utils/auth";
export default {
  onLaunch: function () {
    console.log("App Launch");
  },
  
  onShow: async function(options) {
  	// console.log('App Show')
  	console.log('应用启动路径：', options.path)
  	await this.InSignUnitList();
  	this.startTimer()
  },
  onHide: async function() {
  	// console.log('App Hide')
	// uni.clearStorageSync();
  	this.clearTimer()
  },
  methods: {
	InSignUnitList(){
		if (getToken()) {
			// console.log(uni.getStorageSync('deptId'));
			if(uni.getStorageSync('deptId')){
				this.signDetails(uni.getStorageSync('deptId'))
			}else{
				InSignUnitList().then(res => {
					if(res.data.length){
						this.signDetails(res.data[0].deptId)
						uni.setStorageSync('deptId', res.data[0].deptId);
					}
				})
			}
		}
		
		
	},
	// 签约信息
	signDetails(id) {
		if(uni.getStorageSync('signDetailsId')){
			this.getMessageList(uni.getStorageSync('signDetailsId'))
		}else{
			signDetails({
				deptId:id
			}).then(res => {
				this.getMessageList(res.data.id)
				uni.setStorageSync('signDetailsId', res.data.id);
			})
		}
		
	},
  	// 获取未读消息数量
  	getMessageList(id) {
  		const token = uni.getStorageSync("token") || false
  		if (getToken()) {
  			unreadMessageCount({
				userSignId:id
			}).then(res => {
  				if (res.code == 200) {
  					const num = res.data
  					const numString = String(num)
  					if (num > 99) {
  						this.setBadge('99+')
  					} else if (num > 0) {
  						this.setBadge(numString)
  					} else {
  						this.clearBadge()
  					}
  				}
  			}).catch(error => {
  				this.clearBadge()
  				console.log('失败', error);
  			})
  		} else {
  			this.clearBadge()
  		}
  	},
   
  	//设置角标
  	setBadge(numString) {
  		uni.setTabBarBadge({ //显示数字
  			index: 1, //tabbar下标
  			text: numString//数字
  		})
  	},
  	//清除角标
  	clearBadge() {
  		uni.removeTabBarBadge({
  			index: 1
  		});
  	},
  	startTimer() {
  		this.timer = setInterval(async () => {
  			await this.InSignUnitList();
  		}, 5000);
  	},
  	clearTimer() {
  		if (this.timer) {
  			clearInterval(this.timer);
  			this.timer = null;
  		}
  	},
  },
};

</script>

<style lang="scss">
@import "@/uni_modules/uview-ui/index.scss";
@import "@/static/index.scss";
/*每个页面公共css */
page {
  background-color: #f7f7f7;
  color: #666666;
}
.qidoongye{
	width: 100vw;
	height: 100vh;
	background: #666666;
	position: fixed;
	top: 0;
	left: 0;
}
</style>
